package com.stylefeng.guns.modular.system.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.stylefeng.guns.common.persistence.dao.DeptMapper;
import com.stylefeng.guns.common.persistence.model.Dept;
import com.stylefeng.guns.common.persistence.service.impl.PointServiceImpl;
import com.stylefeng.guns.modular.system.service.IDeptService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Service
@Transactional
public class DeptServiceImpl extends PointServiceImpl<DeptMapper, Dept> implements IDeptService {

    @Resource
    DeptMapper deptMapper;

    @Override
    public void deleteDept(String deptId) {

        Dept dept = deptMapper.selectById(deptId);

        Wrapper<Dept> wrapper = new EntityWrapper<>();
        wrapper = wrapper.like("pids", "%[" + dept.getId() + "]%");
        List<Dept> subDepts = deptMapper.selectList(wrapper);
        for (Dept temp : subDepts) {
            temp.deleteById();
        }

        dept.deleteById();
    }

    @Override
    public List<Dept> getFirstLevelDept() {
        List<Dept> depts = getByPid("0");
        return depts;
    }

    public List<Dept> getByPid(String pid) {
        Map<String,Object> condition = new HashMap<>();
        condition.put("pid",pid);
        return deptMapper.selectByMap(condition);
    }

    @Override
    public List<Dept> getTwoLevelDept(String id) {

        return getByPid(id);
    }

    @Override
    public List<Dept> getTwoLevelDeptByName(String fullName) {
        Map<String,Object> condition = new HashMap<>();
        condition.put("fullName",fullName);
        List<Dept> depts = this.selectByMap(condition);

        return getByPid(depts.get(0).getId());
    }

    @Override
    public List<Dept> getThreeLevelDept(String id) {
        return getByPid(id);
    }




    public void deptSetLevelName(Dept dept){
        String pids = dept.getPids();

        List<String> spPids = Arrays.asList(pids.split(","));

        //一级
        if(spPids.size() == 1){
            dept.setLevelName(dept.getFullname());
        }
        //二级
        if(spPids.size() == 2){
            String pid = spPids.get(1);
            Dept dept1 = new Dept();
            dept1.setId(pid);
            Dept firstLevel = deptMapper.selectById(dept1);

            dept.setLevelName(firstLevel.getFullname() + "," + dept.getFullname());
        }
        //三级
        if(spPids.size() == 3){
            String pid = spPids.get(2);
            Dept dept1 = new Dept();
            dept1.setId(pid);
            Dept threeLevel = deptMapper.selectById(dept1);
            dept.setLevelName(threeLevel.getLevelName() + "," + dept.getFullname());
        }
    }

    private String splitNum(String arg){
        String regEx="[^0-9]";
        Pattern p = Pattern.compile(regEx);
        Matcher m = p.matcher(arg);

        return m.replaceAll("").trim();
    }

    public Map<String,List> getOrgMap(List<Dept> deptList){
        Map<String,List> deptMap = new HashMap<>();
        for (Dept dept : deptList) {
            String pids = dept.getPids();
//            pids = pids.replace("[","").replace("]","");

            String[] spPids = pids.split(",");

            spPids[0] = dept.getId();

            deptMap.put(dept.getLevelName(), Arrays.asList(spPids));
        }

        return deptMap;
    }

    @Override
    public Map<String, List> getOrgMap() {
        List<Dept> depts = deptMapper.selectList(null);
        Map<String, List> orgMap = this.getOrgMap(depts);
        return orgMap;
    }

}
